using System;
using System.Windows.Forms;

namespace CastleSample.UI.Core
{
    public partial class FormMainFrame : MainFrame
    {
        public FormMainFrame()
        {
            InitializeComponent();
        }

        #region IMainFrame Members

        public override MenuItemInfo FindMenuItem(string key)
        {
            ToolStripItem[] items = MainMenuStrip.Items.Find("menuItem" + key, true);
            for (int i = 0; i < items.Length; i++)
            {
                if (items[i] is ToolStripMenuItem)
                {
                    return new MenuItemInfo(items[i] as ToolStripMenuItem);
                }
                else if (items[i] is ToolStripSeparator)
                {
                    return new MenuItemInfo(items[i].Name, "-");
                }
            }
            return null;
        }

        public override bool RegisterMenuItem(MenuItemInfo menuItem)
        {
            MenuItemInfo item = FindMenuItem(menuItem.Key);
            if (item != null)
            {
                return false;
            }
            MainMenuStrip.Items.Add(menuItem.MenuItem);
            return true;
        }

        public override bool RegisterMenuItem(MenuItemInfo menuItem, string relatedMenuKey, MenuItemPosition position)
        {
            bool ret;
            switch (position)
            {
                case MenuItemPosition.Before:
                    ret = RegisterMenuItemBefore(menuItem, relatedMenuKey);
                    break;
                case MenuItemPosition.After:
                    ret = RegisterMenuItemAfter(menuItem, relatedMenuKey);
                    break;
                case MenuItemPosition.ChildFirst:
                    ret = RegisterSubMenuItem(menuItem, relatedMenuKey, true);
                    break;
                case MenuItemPosition.ChildLast:
                    ret = RegisterSubMenuItem(menuItem, relatedMenuKey, false);
                    break;
                default:
                    ret = false;
                    break;
            }
            return ret;
        }


        public override void UnregisterMenuItem(string key)
        {
            MenuItemInfo item = FindMenuItem(key);
            if (item != null)
            {
                ToolStripItem ownerItem = item.MenuItem.OwnerItem;
                if (ownerItem is ToolStripMenuItem)
                {
                    ((ToolStripMenuItem) ownerItem).DropDownItems.Remove(item.MenuItem);
                }
            }
        }

        #endregion

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region Private Method

        private bool RegisterSubMenuItem(MenuItemInfo menuItem, string relatedMenuKey, bool first)
        {
            MenuItemInfo item = FindMenuItem(menuItem.Key);
            if (item != null)
            {
                return false;
            }
            MenuItemInfo parentItem = FindMenuItem(relatedMenuKey);
            if (parentItem == null || parentItem.IsSeparator)
            {
                return false;
            }
            if (first)
            {
                parentItem.MenuItem.DropDownItems.Insert(0, menuItem.MenuItem);
            }
            else
            {
                parentItem.MenuItem.DropDownItems.Add(menuItem.MenuItem);
            }
            return true;
        }

        private bool RegisterMenuItemBefore(MenuItemInfo menuItem, string relatedMenuKey)
        {
            MenuItemInfo item = FindMenuItem(menuItem.Key);
            if (item != null)
            {
                return false;
            }
            MenuItemInfo beforeItem = FindMenuItem(relatedMenuKey);
            if (beforeItem == null)
            {
                return false;
            }
            ToolStripMenuItem ownerItem = beforeItem.MenuItem.OwnerItem as ToolStripMenuItem;
            if (ownerItem == null)
            {
                MainMenuStrip.Items.Insert(MainMenuStrip.Items.IndexOf(beforeItem.MenuItem), menuItem.MenuItem);
            }
            else
            {
                ownerItem.DropDownItems.Insert(ownerItem.DropDownItems.IndexOf(beforeItem.MenuItem), menuItem.MenuItem);
            }
            return true;
        }

        private bool RegisterMenuItemAfter(MenuItemInfo menuItem, string relatedMenuKey)
        {
            MenuItemInfo item = FindMenuItem(menuItem.Key);
            if (item != null)
            {
                return false;
            }
            MenuItemInfo afterItem = FindMenuItem(relatedMenuKey);
            if (afterItem == null)
            {
                return false;
            }
            ToolStripMenuItem ownerItem = afterItem.MenuItem.OwnerItem as ToolStripMenuItem;
            if (ownerItem == null)
            {
                MainMenuStrip.Items.Insert(MainMenuStrip.Items.IndexOf(afterItem.MenuItem) + 1, menuItem.MenuItem);
            }
            else
            {
                ownerItem.DropDownItems.Insert(ownerItem.DropDownItems.IndexOf(afterItem.MenuItem) + 1,
                    menuItem.MenuItem);
            }
            return true;
        }

        #endregion

        private void FormMainFrame_Load(object sender, EventArgs e)
        {
        }

        private void FormMainFrame_MdiChildActivate(object sender, EventArgs e)
        {
            ToolStripManager.RevertMerge(toolStripMain);

            if (ActiveMdiChild != null)
            {
                IToolStripForm childForm = ActiveMdiChild as IToolStripForm;
                if (childForm != null && childForm.MainToolStrip != null)
                {
                    ToolStripManager.Merge(childForm.MainToolStrip, toolStripMain);
                }
            }
        }
    }
}